package com.ruoyi.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.domain.BrandInfo;
import com.ruoyi.domain.GoodsSpuInfo;
import com.ruoyi.mapper.GoodsSpuInfoMapper;
import com.ruoyi.service.IGoodsSpuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-14
 */
@Service
public class GoodsSpuInfoServiceImpl implements IGoodsSpuInfoService
{
    @Autowired
    private GoodsSpuInfoMapper goodsSpuInfoMapper;

    /**
     * 查询商品
     * 
     * @param goodsId 商品主键
     * @return 商品
     */
    @Override
    public GoodsSpuInfo selectGoodsSpuInfoByGoodsId(Long goodsId)
    {
        return goodsSpuInfoMapper.selectGoodsSpuInfoByGoodsId(goodsId);
    }

    /**
     * 查询商品列表
     * 
     * @param goodsSpuInfo 商品
     * @return 商品
     */
    @Override
    public List<GoodsSpuInfo> selectGoodsSpuInfoList(GoodsSpuInfo goodsSpuInfo)
    {
        return goodsSpuInfoMapper.selectGoodsSpuInfoList(goodsSpuInfo);
    }

    /**
     * 新增商品
     * 
     * @param goodsSpuInfo 商品
     * @return 结果
     */
    @Override
    public int insertGoodsSpuInfo(GoodsSpuInfo goodsSpuInfo)
    {
        goodsSpuInfo.setCreateTime(DateUtils.getNowDate());
        return goodsSpuInfoMapper.insertGoodsSpuInfo(goodsSpuInfo);
    }

    /**
     * 修改商品
     * 
     * @param goodsSpuInfo 商品
     * @return 结果
     */
    @Override
    public int updateGoodsSpuInfo(GoodsSpuInfo goodsSpuInfo)
    {
        goodsSpuInfo.setUpdateTime(DateUtils.getNowDate());
        return goodsSpuInfoMapper.updateGoodsSpuInfo(goodsSpuInfo);
    }

    /**
     * 批量删除商品
     * 
     * @param goodsIds 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteGoodsSpuInfoByGoodsIds(Long[] goodsIds)
    {
        return goodsSpuInfoMapper.deleteGoodsSpuInfoByGoodsIds(goodsIds);
    }

    /**
     * 删除商品信息
     * 
     * @param goodsId 商品主键
     * @return 结果
     */
    @Override
    public int deleteGoodsSpuInfoByGoodsId(Long goodsId)
    {
        return goodsSpuInfoMapper.deleteGoodsSpuInfoByGoodsId(goodsId);
    }

    @Override
    public List<GoodsSpuInfo> selectGoodsSpuInfoManyList() {

        List<GoodsSpuInfo> goodsSpuInfoList = goodsSpuInfoMapper.selectGoodsSpuInfoManyListAll();

        for (GoodsSpuInfo spu : goodsSpuInfoList) {
            System.out.println("spu = " + spu.getSupplierIdsStr());

            List<Integer> ids = new ArrayList<>();
            if (spu.getSupplierIdsStr() != null) {

                String[] ss = spu.getSupplierIdsStr().split(",");

                for (String s : ss) {
                    ids.add(Integer.parseInt(s));
                }
                // 2.将 id的list set给 品牌
            }
            spu.setSupplierIds(ids);
        }

        return goodsSpuInfoList;
    }
}
